package com.fenghuajueli.module_host;

import android.content.Context;
import android.graphics.Color;
import android.view.View;

import com.blankj.utilcode.util.ConvertUtils;
import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView;

public class BaseTabAdapter extends CommonNavigatorAdapter {

    private String[] tabTitleList;
    private OnListClickListener<Integer> onListClickListener;
    private MagicIndicator magicIndicator;
    private int unselectColor = Color.parseColor("#999999");
    private int selectColor = Color.WHITE;
    private IPagerIndicator iPagerIndicator;

    public BaseTabAdapter(MagicIndicator magicIndicator, String[] tabTitleList) {
        this.tabTitleList = tabTitleList;
        this.magicIndicator = magicIndicator;
    }

    public void setOnListClickListener(OnListClickListener<Integer> onListClickListener) {
        this.onListClickListener = onListClickListener;
    }

    @Override
    public int getCount() {
        return tabTitleList.length;
    }

    public void setUnselectColor(int unselectColor) {
        this.unselectColor = unselectColor;
    }

    public void setSelectColor(int selectColor) {
        this.selectColor = selectColor;
    }

    public void setiPagerIndicator(IPagerIndicator iPagerIndicator) {
        this.iPagerIndicator = iPagerIndicator;
    }

    @Override
    public IPagerTitleView getTitleView(Context context, int index) {

        ClipPagerTitleView clipPagerTitleView = new ClipPagerTitleView(context);
        clipPagerTitleView.setText(tabTitleList[index]);
        clipPagerTitleView.setTextColor(unselectColor);
        clipPagerTitleView.setClipColor(selectColor);
        clipPagerTitleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                magicIndicator.onPageSelected(index);
                magicIndicator.onPageScrolled(index, 0.0F, 0);
                if (onListClickListener != null) {
                    onListClickListener.itemClick(index, index);
                }
            }
        });
        clipPagerTitleView.setPadding(ConvertUtils.dp2px(30), 0, ConvertUtils.dp2px(30), 0);
        return clipPagerTitleView;
    }

    @Override
    public IPagerIndicator getIndicator(Context context) {
        return iPagerIndicator;
    }
}
